package service

import (
	"errors"
	"study/go-blog/dao"
	"study/go-blog/models"
	"study/go-blog/utils"
)

func Login(userName, password string) (*models.LoginRes, error) {
	password = utils.Md5Crypt(password, "mszlu")

	user := dao.GetUser(userName, password)

	if user == nil {
		return nil, errors.New("账号密码不正确")
	}
	uid := user.Uid

	token, err := utils.Award(&uid)
	if err != nil {
		return nil, errors.New("token生成失败")
	}
	var userInfo models.UserInfo

	userInfo.Uid = user.Uid
	userInfo.UserName = user.UserName
	userInfo.Avatar = user.Avatar

	var lr = &models.LoginRes{
		token,
		userInfo,
	}
	return lr, nil
}
